<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Pisyek Kumar
 * @email pisyek@gmail.com
 * @link http://www.pisyek.com
 */
class Blog_model extends CI_Model {

    function convert_datetime($mysql_tatetime = null){
        list($date, $time) = explode(' ', $mysql_tatetime);
        list($year, $month, $day) = explode('-', $date);
        list($hour, $minute, $second) = explode(':', $time);
 
        $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
        return date('r', $timestamp);
    }
    
    function get_last_post_date() {
        $this->db->select('blog_entry.entry_date');
        $this->db->from('blog_entry');
        $this->db->order_by('blog_entry.entry_id', 'DESC');
        $this->db->limit(1);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            $result = $query->row();
            return $this->convert_datetime($result->entry_date);
        }
    }
    
    function get_posts($resultados = 5, $pagina = 0) {
        $this->db->order_by('entry_date', 'desc'); // get all entry, sort by latest to oldest
        $this->db->join('usuario', 'blog_entry.author_id = usuario.usu_idusuario');
        $this->db->limit(($pagina + 1) * $resultados, ($pagina * $resultados));
        $query = $this->db->get('blog_entry');
        log_message('error', 'pepito: '.$query->num_rows());
        return $query->result();
    }
    
    function get_num_posts(){
        $this->db->from('blog_entry');
        return $this->db->count_all_results();
    }

    function add_new_entry($author, $name, $body, $categories) {
        $data = array(
            'author_id' => $author,
            'entry_name' => $name,
            'entry_body' => $body,
        );
        $this->db->insert('blog_entry', $data);

        $object_id = (int) mysql_insert_id(); // get latest post id

        foreach ($categories as $category) {
            $relationship = array(
                'object_id' => $object_id, // object id is post id
                'category_id' => $category,
            );
            $this->db->insert('blog_entry_relationships', $relationship);
        }
    }

    function add_new_comment($post_id, $commentor, $email, $comment, $website) {
        $total_count = 0;

        $data = array(
            'entry_id' => $post_id,
            'comment_name' => $commentor,
            'comment_email' => $email,
            'comment_body' => $comment,
            'comment_website' => $website,
            'user_id' => $this->session->userdata('idusuario'),
        );
        $this->db->insert('blog_comment', $data);

        $query = $this->get_post($post_id);

        foreach ($query as $post) {
            $total_count = $post->comment_count + 1;
        }

        $count = array(
            'comment_count' => $total_count,
        );

        $this->db->where('entry_id', $post_id);
        $query = $this->db->update('blog_entry', $count); // update comment count
    }

    function get_post($id) {
        $this->db->where('entry_id', $id);
        $this->db->order_by('entry_date', 'desc');
        $this->db->join('usuario', 'blog_entry.author_id = usuario.usu_idusuario');
        $query = $this->db->get('blog_entry');
        if ($query->num_rows() !== 0) {
            return $query->result();
        }
        else
            return FALSE;
    }
    
    function get_post_by_dates($year = false, $month = false) {
        if($month != false){
            $this->db->where('MONTH(entry_date)', $month);
        }
        $this->db->where('YEAR(entry_date)', $year);
        $this->db->order_by('entry_date', 'desc');
        $query = $this->db->get('blog_entry');
        if ($query->num_rows() !== 0) {
            return $query->result();
        }
        else
            return FALSE;
    }

    function get_post_comment($post_id, $resultados = 10, $pagina = 0) {
        $this->db->where('entry_id', $post_id);
        $this->db->limit(($pagina + 1) * $resultados, ($pagina * $resultados));
        $query = $this->db->get('blog_comment');
        return $query->result();
    }
    
    function get_lasts_comment($resultados = 5, $pagina = 0) {
        $this->db->limit(($pagina + 1) * $resultados, ($pagina * $resultados));
        $this->db->order_by('comment_date', 'desc');
        $query = $this->db->get('blog_comment');
        return $query->result();
    }

    function total_comments($id) {
        $this->db->like('entry_id', $id);
        $this->db->from('comment');
        return $this->db->count_all_results();
    }

    function add_new_category($name, $slug) {
        $i = 0;
        $slug_taken = FALSE;

        while ($slug_taken == FALSE) { // to avoid duplicate slug
            $category = $this->get_category(NULL, $slug);
            if ($category == FALSE) {
                $slug_taken = TRUE;
                $data = array(
                    'category_name' => $name,
                    'slug' => $slug,
                );
                $this->db->insert('blog_entry_category', $data);
            }
            $i = $i + 1;
            $slug = $slug . '-' . $i;
        }
    }

    function get_category($id = FALSE, $slug) {
        if ($id != FALSE)
            $this->db->where('category_id', $id);
        elseif ($slug)
            $this->db->where('slug', $slug);

        $query = $this->db->get('blog_entry_category');

        if ($query->num_rows() !== 0) {
            return $query->result();
        }
        else
            return FALSE; // return false if no category in database
    }

    function get_categories($array = false) {        
        $query = $this->db->get('blog_entry_category');
        if($array){
            $data = array();
            foreach ($query->result_array() as $value){
                $data[$value['category_id']] = $value['category_name'];
            }
            return $data;
        }
        return $query->result();
    }
    
    

    function get_related_categories($post_id) {
        $category = array();

        $this->db->where('object_id', $post_id);
        $query = $this->db->get('blog_entry_relationships'); // get category id related to the post

        foreach ($query->result() as $row) {
            $this->db->where('category_id', $row->category_id);
            $query = $this->db->get('blog_entry_category'); // get category details
            $category = array_merge($category, $query->result());
        }

        return $category;
    }

    function get_category_post($slug, $year = false, $month = false) {
        $list_post = array();

        if($year == false && $month == false){
            $this->db->where('slug', $slug);
        }
        $query = $this->db->get('blog_entry_category'); // get category id
        if ($query->num_rows() == 0)
            show_404();

        foreach ($query->result() as $category) {
            $this->db->where('category_id', $category->category_id);
            $query = $this->db->get('blog_entry_relationships'); // get posts id which related the category
            $posts = $query->result();
        }

        if (isset($posts) && $posts) {
            foreach ($posts as $post) {
                if($year != false){
                    $list_post = array_merge($list_post, $this->get_post_by_dates($year, ($month ? $month : false))); // get posts and merge them into array
                }else{
                    $list_post = array_merge($list_post, $this->get_post($post->object_id)); // get posts and merge them into array
                }
            }
        }

        return $list_post; // return an array of post object
    }
    
    function get_years_post(){
        $list_post = array();
        
        
        $this->db->order_by('entry_date', 'desc');
        $query = $this->db->get('blog_entry');
        
        if ($query->num_rows() !== 0) {
            $posts = $query->result();
        }
        else
            return FALSE;
        
        $list_post = array_merge($list_post, $posts);

        return $list_post; // return an array of post object
    }
    
    function delete_post($id = false){
        $this->db->delete('blog_entry', array('entry_id' => $id)); 
        $this->db->delete('blog_entry_relationships', array('object_id' => $id)); 
        $this->db->delete('blog_comment', array('entry_id' => $id)); 
    }
}

/* End of file blog_model.php */
/* Location: ./application/models/blog_model.php */